package com.ktr.service.impl;


import com.ktr.entity.*;
import com.ktr.service.KtrAddressService;
import com.ktr.service.KtrInfoDataService;
import com.ktr.service.KtrInfoService;
import com.ktr.service.KtrTypeService;
import com.ktr.utils.Device;
import com.ktr.utils.DeviceUtils;
import com.ktr.utils.KtrUtils;
import com.bhudy.base.BaseServiceImpl;
import com.bhudy.base.QueryFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;

/**
 * @Author com.bhudy
 * @Description KtrInfoServiceImpl
 * @Date 2019-10-30 23:09:37
 */
@Service
public class KtrInfoServiceImpl extends BaseServiceImpl<KtrInfo, Serializable> implements KtrInfoService {

    @Autowired
    private KtrInfoDataService ktrInfoDataService;
    @Autowired
    private KtrAddressService ktrAddressService;
    @Autowired
    private KtrTypeService ktrTypeService;

    @Override
    public void runSerialPort() {
        QueryFilter queryFilter = new QueryFilter();
        // 获取设备端口信息
        queryFilter.clear().and("status", 0);
        List<KtrAddress> ktrAddressList = ktrAddressService.findList(queryFilter);
        Map<Long, Object[]> ktrAddressMap = new HashMap<>();
        for (KtrAddress ktrAddress : ktrAddressList) {
            Object[] objs = {ktrAddress.getKtrAddressType(), ktrAddress.getKtrIp(), ktrAddress.getKtrPort()};
            ktrAddressMap.put(ktrAddress.getKtrId(), objs);
        }

        // 获取设备类型
        queryFilter.clear().and("status", 0);
        List<KtrType> ktrTypeList = ktrTypeService.findList(queryFilter);
        Map<Long, String> ktrTypeMap = new HashMap<>();
        for (KtrType ktrType : ktrTypeList) {
            ktrTypeMap.put(ktrType.getKtrId(), ktrType.getKtrInstruct());
        }

        // 获取设备类型
        queryFilter.clear().and("status", 0);
        List<KtrInfoData> ktrInfoDataList = ktrInfoDataService.findList(queryFilter);
        Map<Long, Map<Integer, Double>> ktrInfoDataMap = new HashMap<>();
        for (KtrInfoData ktrInfoData : ktrInfoDataList) {
            if (ktrInfoDataMap.containsKey(ktrInfoData.getKtrInfoId())) {
                ktrInfoDataMap.get(ktrInfoData.getKtrInfoId()).put(ktrInfoData.getPos(), ktrInfoData.getKtrData());
            } else {
                Map<Integer, Double> deviceDataMap = new HashMap<>();
                deviceDataMap.put(ktrInfoData.getPos(), ktrInfoData.getKtrData());
                ktrInfoDataMap.put(ktrInfoData.getKtrInfoId(), deviceDataMap);
            }
        }

        queryFilter.clear().and("status", 0);
        List<KtrInfo> ktrInfoList = super.findList(queryFilter);
        for (KtrInfo ktrInfo : ktrInfoList) {
            Object[] objs = ktrAddressMap.get(ktrInfo.getKtrAddressId());
            Device device = new Device();
            device.setAddress((String) objs[1]);
            device.setPort((int) objs[2]);
            device.setKey(device.getAddress() + "_" + device.getPort());
            device.setInstruct(KtrUtils.getCRC(ktrInfo.getKtrCode() + ktrTypeMap.get(ktrInfo.getKtrTypeId())));
            device.setKtrId(ktrInfo.getKtrId());
            device.setCode(ktrInfo.getKtrCode());
            device.setStart(ktrInfo.getStart());
            device.setEnd(ktrInfo.getEnd());
            device.setStep(ktrInfo.getStep());
            device.setDeviceDataMap(ktrInfoDataMap.get(ktrInfo.getKtrId()));

            if ((int) objs[0] == 0) {
                DeviceUtils.getInstance().getDeviceList().add(device);
                DeviceUtils.getInstance().openSocket(device.getAddress(), device.getPort());
            }
        }

        Executors.newCachedThreadPool().execute(() -> {
            while (true) {
                ktrInfoDataService.updateKtrInfoData();
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }

}
